package com.ys.controller.api.advanced;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ys.entity.advanced.AdsSpPurchasedProduct;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.mapper.sys.YsColMapper;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.util.dhecp.StringUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 非广告商品订单情况表
 */
@Controller
@RequestMapping("/api/inter/adsSpPurchasedProduct")
public class ApiInterAdsSpPurchasedProductController {
	private static Long MENU_ID = 118L; //


	@Autowired
	private CommonApiService<AdsSpPurchasedProduct> commonApiService;

	@Autowired
	private YsColMapper ysColMapper;

	@Autowired
	private DiySqlMapper diySqlMapper;

	@RequiresPermissions("adsSpPurchasedProduct:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute AdsSpPurchasedProduct cond,String conditions){
		cond.setOrderby("A.Date DESC,A.ShopGuid ASC");
		cond = commonApiService.dataAuth(MENU_ID, cond);

		LayuiTableVoJA vo = commonApiService.searchLay(cond);
		{
			JSONObject JO = new JSONObject();
			//金额累计
			Map<String, Object> map = cond.setFields(
					"ISNULL(SUM(A.SalesOtherSku1d),0) AS salesOtherSku1d," +
					" ISNULL(SUM(A.UnitsSoldOtherSku1d),0) AS unitsSoldOtherSku1d," +
					" ISNULL(SUM(A.PurchasesOtherSku1d),0) AS purchasesOtherSku1d"

			)
					.queryMapNoOrder().get(0);
			JO = JSON.parseObject(JSON.toJSONString(map));
			vo.setTotalRow(JO);
		}
		return vo;
	}

	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute AdsSpPurchasedProduct cond, String conditions) throws Exception{
		cond = commonApiService.dataAuth(MENU_ID, cond);
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.setOrderby("A.Date DESC,A.ShopGuid ASC").queryMap();

		List<YsCol> listField = new YsCol()
				.setMenuId(MENU_ID)
				.setFrontShow(YsColEnum.FrontShow.SHOW.value)
				.setOrderby("A.SortNo asc")
				.queryList();


		ExportExcelUtils<AdsSpPurchasedProduct> util = new ExportExcelUtils<AdsSpPurchasedProduct>(new AdsSpPurchasedProduct());

		String toFilePath = util.exportExcel(listData, listField, "非广告商品订单情况表");
		UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}

	@RequestMapping("/test7")
	@ResponseBody
	public void test7(HttpServletRequest request, HttpServletResponse response) throws InterruptedException {
		//根据数据库新增字段pc
		List<Map<String, Object>> ads_sp_advertisedProduct = diySqlMapper.searchPC("ADS_Sp_AdvertisedProduct");
		for (int i = 0; i < ads_sp_advertisedProduct.size(); i++) {
			YsCol ysCol = new YsCol();
			ysCol.setMenuId(117L);
			ysCol.setName(String.valueOf(ads_sp_advertisedProduct.get(i).get("value")));
			ysCol.setCode(StringUtils.uncapitalize(String.valueOf(ads_sp_advertisedProduct.get(i).get("name"))));
			ysCol.setSortNo(i);
			ysCol.setAuthType(null);
			ysCol.setFrontType("t_text");
			ysCol.setFrontEdit(0);
			ysCol.setFrontShow(1);
			ysCol.setFrontWidth(120);
			ysCol.setFrontFixed(0);
			ysCol.setFrontAlign(null);
			ysCol.setFrontConditionState(0);
			ysCol.setFrontConditionType("t_text");
			ysCol.setFrontConditionRangeType("t_system");
			ysCol.setFrontConditionCode("");
			ysCol.setFrontConditionDefault("");
			ysCol.setDictKeyBase(null);
			ysCol.setFormAddState(0);
			ysCol.setFormAddType("t_text");
			ysCol.setFormAddRequired(0);
			ysCol.setFormAddReadonly(null);
			ysCol.setFormEditState(0);
			ysCol.setFormEditType("t_text");
			ysCol.setFormEditRequired(0);
			ysCol.setFormEditReadonly(null);
			ysCol.setModalKey(null);
			ysCol.setModalRelativeCode(null);
			ysCol.setModalValueCode(null);
			ysColMapper.insert(ysCol);
		}

	}
}
